<template>
	<view class="content" v-if="show">
		<view v-if="flag == false">
			<view class="ipts">
				<view class="info-item">
					<view class="dot"></view>
					<span>气表号</span>
					<input style="color: #999;font-size: 28rpx;" v-model="meter_num" disabled type="text"
						placeholder="待同步" placeholder-class="place-class">
				</view>
				<view class="hengxian"></view>
				<view class="info-item">
					<view class="dot"></view>
					<span>上期表数</span>
					<input style="color: #999;font-size: 28rpx;" v-model="last_gas_num" disabled type="text"
						placeholder="待同步" placeholder-class="place-class">
				</view>
				<view class="hengxian"></view>
				<view class="info-item">
					<view class="info-item-left">
						<view class="dot"></view>
						<span>上期抄表日期</span>
					</view>
					<text style="color: #999;font-size: 28rpx;">{{last_gas_time}}</text>
				</view>
				<view class="hengxian"></view>
				<view class="info-item">
					<view class="dot"></view>
					<span>本期表数</span>
					<input style="color: #999;font-size: 28rpx;" disabled type="text"
						:placeholder="this_gas_num?'已上传':'待同步'" placeholder-class="place-class">
				</view>
				<view class="hengxian"></view>
				<template v-if="this_gas_time">
					<view class="info-item">
						<view class="info-item-left">
							<view class="dot"></view>
							<span>本期抄表日期</span>
						</view>
						<text style="color: #999;font-size: 28rpx;">{{this_gas_time}}</text>
					</view>
					<view class="hengxian"></view>
				</template>
				<view class="info-item">
					<view class="info-item-left">
						<view class="dot"></view>
						<span>本期表数照片</span>
					</view>
					<view class="danger-img-box">
						<!--  -->
						<view class="danger-img" v-if="!img1">
							<image class="add" src="/static/updatePhoto.png" mode="" @click="flag = true"></image>
						</view>
						<!--  -->
						<view class="danger-img" v-else>
							<image class="realImg" :src='img1' @tap="seeimg(img1)"></image>
						</view>
					</view>

				</view>
				<view class="hengxian"></view>
				<!-- 期限 -->
				<view class="info-item">
					<view style="display: flex;align-items: center;">
						<view class="dot"></view>
						<span>上传期限</span>
					</view>
					<input style="color: #999;font-size: 28rpx;" v-model="time_limit" disabled type="text"
						:placeholder="placeholders" placeholder-class="place-class">
				</view>
			</view>
		</view>
		<!-- 本期表数跳转上传 -->
		<view class="bsUpdates" v-if="flag">
			<view class="updateText">拍照自动识别气表数，如识别不正确请手动修改</view>
			<input v-model="gas_num"
				style="border-bottom: 2rpx solid #BBBBBB;width: 50%;margin: 0 auto; margin-top: 92rpx;margin-bottom: 146rpx;"
				type="text" value="" />
			<view style="margin-left: 40rpx;margin-bottom: 50rpx;">
				<text style="color: red;">注意:</text>
				<text>气表数只填写黑色方框内的数字，请确保气表数与上传照片一致！</text>
			</view>
			<view class="updateImg">
				<view class="update-imgs" v-if="!img1" @click="handleUpdataImg">
					<image class="add" src="/static/pzsb.png" mode="" @click="flag = true"></image>
				</view>
				<view class="danger-imgs" v-else>
					<image class="realImgs" :src='img1' mode="" @tap="seeimg(img1)"></image>
					<view class="updeletes" @tap="updetail4()">
						<image src="/static/delete.png"></image>
					</view>
				</view>
			</view>
			<view class="btns">
				<view class="btn1 consel" @click="numClous">取消</view>
				<view class="btn1 save" @click="numberVation">保存</view>
			</view>
			<view class="zhezhao" v-if="shots"></view>
			<view class="tscont" v-if="shots">
				<image src="/static/tsy.png"></image>
				<view class="close" @tap="closes">关闭</view>
			</view>
		</view>

		<!-- <view class="btn">立即登录</view> -->
		<cpimg ref="cpimg" @result="cpimgOk" @err="cpimgErr" :number="1" :fixOrientation="true" :size="500"
			:maxWidth="800" :ql="0.7" type="url" />



	</view>
</template>

<script>
	import cpimg from "@/components/cpimg/cpimg.vue"
	export default {
		components: {
			cpimg
		},
		data() {
			const currentDate = this.getDate({
				format: true
			})
			return {
				show: true,
				shots: true,
				img1: null, //处理照片的路径
				date: currentDate,
				showR: true,
				username: '',
				password: '',
				index: 0,
				userinfo: {},
				value: '', //搜索关键词
				installList: [], //安装列表
				dosearch: false,
				showAcount: false,
				page: 1,
				// 上传图片控制
				flag: false,
				// 气表号
				gas_num: '',
				token: '',
				// 是否禁用气表数框
				disableds: true,
				// 上传期限开始与结束时间
				start_date: currentDate,
				end_date: currentDate,
				other: currentDate,
				// 上传期限
				term: '',
				placeholders: '',
				last_gas_num: '',
				this_gas_num:'',
				last_gas_time: '',
				this_gas_time:'',
				meter_num: '',
				time_limit: '',
				fileImg: '',
				obj: {},
				ban:true,
			};
		},
		computed: {
			startDate() {
				return this.getDate('start');
			},
			endDate() {
				return this.getDate('end');
			}
		},
		onLoad(e) {
			this.getInstallDetail()
		},
		onReady() {

		},
		onShow() {
			uni.getStorage({
				key: 'token'
			}).then(da => {
				this.token = da[1].data
			})
			if (this.obj.img) {
				this.img1 = this.obj.img;
				this.flag = true;
				this.shots = false;
				this.gas_num = this.obj.gas_num
				this.this_gas_num = this.obj.gas_num
			}
			console.log(this.obj);
		},

		methods: {
			updetail4() {
				this.img1 = ''
			},
			//图片预览
			seeimg(img) {
				uni.previewImage({
					urls: [img],
					longPressActions: {
						itemList: [],
						success: function(data) {
							console.log('选中了第' + (data.tapIndex + 1) + '个按钮,第' + (data.index + 1) + '张图片');
						},
						fail: function(err) {
							console.log(err.errMsg);
						}
					}
				});
			},



			cpimgOk(file) {
				let that = this
				var ret = that.ret
				uni.showLoading({
					title: '上传中...'
				})
				uni.uploadFile({
					url: that.$fileImg + '/common/upload',
					filePath: file[0],
					name: 'file',
					formData: {
						token: that.token
					},
					success(res1) {
						let dataPath = JSON.parse(res1.data)
						if (dataPath.code == 1) {
							that.fileImg = dataPath.data.url
							console.log(dataPath.data.url)
							that.img1 = dataPath.data.intact_url;
							uni.hideLoading()
							that.$request.request({
								url: '/Outuser/get_pic_num',
								method: 'POST',
								data: {
									path: that.fileImg,
									token: that.token
								}
							}).then(resolt => {
								if (resolt.data.code == 1) {
									uni.showToast({
										title: resolt.data.msg,
										icon: 'none'
									})
									that.gas_num = resolt.data.data;
									uni.hideLoading()

								} else {
									uni.showToast({
										title: '图片未识别出来气表数，请手动填写',
										icon: 'none'
									})
									// that.disableds = false
									uni.hideLoading()
								}
							})
						}
						setTimeout(() => {
							uni.hideLoading()
						}, 4000)
					},
					fail: () => {
						uni.hideLoading()
						uni.showToast({
							title: '上传失败'
						})
					}
				});


			},


			cpimgErr(e) {
				uni.showToast({
					title: '上传失败',
					icon: 'none'
				})
				console.log(e)
			},
			// 选择图片
			handleUpdataImg() {
				console.log("000000000000000")
				var that = this;
				uni.navigateTo({
					url: '/pages/meter_fild/company'

				})
				//图片压缩插件
				// that.$refs.cpimg._changImg()

			},

			closes() {
				this.shots = false
			},
			bindDateChange1: function(e) {
				this.start_date = e.target.value
			},
			bindDateChange2: function(e) {
				this.end_date = e.target.value;
				this.other = this.end_date;
			},
			// 选择日期
			bindDateChange: function(e) {
				this.last_gas_time = e.target.value
			},
			getDate(type) {
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();

				if (type === 'start') {
					year = year - 60;
				} else if (type === 'end') {
					year = year + 2;
				}
				month = month > 9 ? month : '0' + month;;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}-${day}`;
			},
			// 气表数弹框保存
			numPosit() {
				if (this.gas_num == '') {
					uni.showToast({
						title: '请填写气表数',
						icon: 'none'
					})
					return
				}
				if (this.img1 == null) {
					uni.showToast({
						title: '请上传气表照片',
						icon: 'none'
					})
					return
				}
				this.flag = false
			},
			// 取消气表弹框
			numClous() {
				// this.img1 = null
				// this.gas_num = ''
				this.flag = false
			},
			showRecord() {
				this.showR = !this.showR
			},
			searchFocus() {
				this.showAcount = false
			},
			infoClick() {
				this.showAcount = !this.showAcount
			},
			// 退出登录
			logOut() {
				uni.clearStorage({
					key: 'logininfo',
					success: () => {
						this.showAcount = false
						uni.redirectTo({
							url: '../../login/login'
						})
					}
				})
			},
			// 跳转至记录页面
			toJiLu() {
				uni.navigateTo({
					url: './jilu'
				})
			},
			// 跳转用户信息页面
			toCaiLiao(id) {
				uni.navigateTo({
					url: '../userinfo/uninstall?id=' + id
				})
			},
			// 点击获取安装数据
			searchUser(val) {
				// this.getInstallDetail ()
				this.dosearch = true
				uni.navigateTo({
					url: '../query/query?key=' + val
				})
			},
			// 获取安装数据
			async getInstallDetail() {
				let that = this
				that.$request.request({
					url: '/outuser/gas_get',
					method: 'POST',
					data: {
						token: uni.getStorageSync('token')
					}
				}).then(res => {
					if (res.data.code == 1) {
						that.meter_num = res.data.data.meter_num
						that.last_gas_num = res.data.data.last_gas_num
						that.last_gas_time = res.data.data.last_gas_time
						that.this_gas_num = res.data.data.this_gas_num
						that.time_limit = res.data.data.time_limit
						that.img1 = res.data.data.this_gas_image
						that.this_gas_time = res.data.data.create_time
					} else {
						this.$showToast(res.data.msg)
						this.show = false
					}
				})

			},
			// 删除图片
			deletImg() {
				this.img1 = ''
				this.disableds = true
			},
			saveUpload() {
				let that = this
				uni.uploadFile({
					url: that.$fileImg + '/upload/upload',
					filePath: that.img1,
					name: 'file',
					formData: {
						token: uni.getStorageSync('token')
					},
					success: (res) => {

						let dataPath = JSON.parse(res.data)
						if (dataPath.code == 1) {
							that.$request.request({
								url: '/outuser/gas_up',
								method: 'POST',
								data: {
									gas_num: that.gas_num,
									image: dataPath.data.imgurl.intact_url,
									token: that.token
								}
							}).then(res => {
								uni.showToast({
									title: res.data.msg,
									icon: 'none'
								})
								if (res.data.code == 1) {
									// this.flag = false
									setTimeout(()=>{
										uni.navigateBack({
											delta:1
										})
									},1000)
								}
							})
						} else {
							that.$showToast(dataPath.msg)
						}
					}
				})
			},
			// 保存气表数
			numberVation() {
				let that = this
				if (that.gas_num == '') {
					uni.showToast({
						title: '请填写气表数',
						icon: 'none'
					})
					return
				}
				console.log(that.img1)
				if (that.img1 == null) {
					uni.showToast({
						title: '请上传气表照片',
						icon: 'none'
					})
					return
				}
				if(this.ban){
					this.ban = false
					this.saveUpload()
				}else{
					this.$showToast('正在提交中,请勿连点...')
					setTimeout(()=>{
						this.ban = true
					},10000)
				}


			},
			// 取消保存气表数
			numberFalse() {
				this.img1 = null
				this.gas_num = ''
				this.flag = false
			},

		},

	}
</script>

<style lang="scss" scoped>
	page {
		background-color: #F5F5F5;
		// padding-bottom: 200rpx;
		// margin-bottom: 150rpx;
		overflow-x: hidden;
	}

	.danger-imgs {
		position: relative;

		.updeletes {
			position: absolute;
			top: -30rpx;
			right: -40rpx;
			width: 60rpx;
			height: 60rpx;
			z-index: 88;

			image {
				width: 100%;
				height: 100%;
			}
		}
	}

	.zhezhao {
		position: fixed;
		top: 0;
		left: 0;
		height: 100%;
		width: 100%;
		background: rgba(0, 0, 0, 0.5);
		z-index: 992;
	}

	.tscont {
		position: fixed;
		z-index: 999;
		height: 100%;
		width: 100%;
		top: 0;
		left: 0;
		text-align: center;
		padding: 140rpx 0;

		image {
			height: 800rpx;
			margin: 0 auto;
		}

		.close {
			margin: 40rpx auto;
			height: 60rpx;
			line-height: 60rpx;
			font-size: 28rpx;
			color: #fff;
			width: 200rpx;
			border: 1px solid #fff;
			border-radius: 10rpx;
		}
	}

	.content {
		position: relative;
		padding-bottom: 140rpx;
	}

	.uni-input {
		padding: 0;
	}

	// header
	.header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 30rpx 30rpx;
		background-color: #fff;

		.header-left {
			display: flex;
			align-items: center;
			font-size: 32rpx;
			font-weight: 500;
			color: #333;

			image {
				width: 28rpx;
				height: 28rpx;
				margin-right: 20rpx;
			}
		}

		.header-right {
			display: flex;
			align-items: center;
			font-size: 28rpx;
			color: #333;

			image {
				width: 52rpx;
				height: 52rpx;
				margin-right: 10rpx;
			}
		}
	}

	.ipts {
		background-color: #fff;
		margin-top: 30rpx;
	}

	.info-item {
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: space-between;
		// height: 72rpx;
		width: 100%;
		padding: 10rpx 30rpx;
		background-color: #fff;
		font-size: 28rpx;
		color: #333;
		height: 100rpx;
		line-height: 100rpx;

		span {
			font-size: 28rpx;
		}

		input {
			flex: 1;
			text-align: right;
		}

		.dot {
			width: 8rpx;
			height: 8rpx;
			background-color: #003690;
			border-radius: 50%;
			margin-right: 10rpx;
		}

		.danger-img {
			position: relative;
			width: 75rpx;
			height: 75rpx;
			border: 2rpx dashed #eee;
			margin-right: 10rpx;

			.add {
				width: 32rpx;
				height: 32rpx;
				position: absolute;
				left: 50%;
				top: 50%;
				transform: translate(-50%, -50%);
			}

			.realImg {
				width: 100%;
				height: 100%;
			}

			.delet {
				position: absolute;
				top: -12rpx;
				right: -12rpx;
				width: 28rpx;
				height: 28rpx;
				z-index: 999;
			}

		}

		.danger-img-box {
			display: flex;
			align-items: center;
		}

		.info-item-left {
			display: flex;
			align-items: center;
			font-size: 32rpx;
		}
	}

	.hengxian {
		width: 700rpx;
		height: 2rpx;
		background-color: #eee;
		margin: 0rpx auto;
	}

	.place-class {
		font-size: 26rpx;
		color: #999;
	}

	.btn {
		width: 646rpx;
		height: 80rpx;
		margin: 74rpx auto;
		text-align: center;
		line-height: 80rpx;
		border-radius: 40rpx;
		background-image: linear-gradient(to bottom, #2366D5, #001B48);
		font-size: 32rpx;
		color: #E5EFFE;
	}

	.btns {
		position: fixed;
		bottom: 0;
		display: flex;
		width: 100%;

		.btn1 {
			width: 50%;
			height: 96rpx;
			line-height: 96rpx;
			text-align: center;
			font-size: 28rpx;
		}

		.consel {
			background-color: #e9e9e9;
			color: #777;
		}

		.save {
			background-color: #003690;
			color: #fff;
		}
	}

	.btn:active {
		background: rgba(0, 0, 0, 0.4);
	}

	.record {
		display: flex;
		align-items: center;
		color: #003690;
		font-size: 28rpx;
		margin-top: 20rpx;

		image {
			width: 32rpx;
			height: 32rpx;
			margin: 0 20rpx 0 30rpx;
		}

		span {
			font-weight: 700;
		}
	}

	.record-item {
		display: flex;
		justify-content: space-between;
		padding: 10rpx 30rpx;
		border-bottom: 2rpx solid #eee;

		.record-item-left {
			display: flex;
			align-items: center;

			.shu {
				width: 8rpx;
				height: 28rpx;
				background-color: #003690;
				border-radius: 8rpx;
			}

			.title {
				font-size: 32rpx;
				color: #333;
				font-weight: 500;
				margin: 0 10rpx;
			}

			.time {
				font-size: 28rpx;
				color: #777;
			}
		}

		.record-item-right {
			display: flex;
			align-items: center;

			.dianhua {
				width: 28rpx;
				height: 28rpx;
			}

			.zhankai {
				width: 36rpx;
				height: 36rpx;
			}

			.title {
				font-size: 32rpx;
				color: #555;
				font-weight: 500;
				margin: 0 10rpx;
			}
		}
	}

	.bsUpdates {
		.updateText {
			width: 100%;
			height: 40rpx;
			font-size: 32rpx;
			text-align: left;
			color: #ff0000;
			line-height: 12px;
			margin: 40rpx;
		}

		.updateXian {
			margin: 0 auto;
			margin-top: 92rpx;
			margin-bottom: 146rpx;
			border: 1rpx solid #bbbbbb;
			width: 50%;
		}

		.updateImg {
			width: 460rpx;
			height: 280rpx;
			margin: 0 auto;

			.update-imgs {
				width: 100%;
				height: 100%;

				image {
					width: 100%;
					height: 100%;
				}
			}

			.danger-imgs {
				width: 100%;
				height: 100%;
				position: relative;

				.delets {
					width: 40rpx;
					height: 40rpx;
					position: absolute;
					top: -44rpx;
					left: 0;
				}

				.realImgs {
					width: 100%;
					height: 100%;
				}
			}
		}

		.btns {
			position: fixed;
			bottom: 0;
			display: flex;
			width: 100%;

			.btn1 {
				width: 50%;
				height: 96rpx;
				line-height: 96rpx;
				text-align: center;
				font-size: 28rpx;
			}

			.consel {
				background-color: #e9e9e9;
				color: #777;
			}

			.save {
				background-color: #003690;
				color: #fff;
			}
		}
	}
</style>
